// 注册页的业务模块
console.log( '注册页的业务模块。。。' )

import axios from 'axios'
import baseUrl from './webpack.env.js'

// 点击发送验证码
$('.btn-success').click(function (){
    // 获取输入的邮箱
    let email = $('#email').val() // '570062873@qq.com'
    // 验证邮箱格式
    // '345634534@qq.com'
    // 'wangbaoqiang_123@sohu.com.cn'
    let regEmail = /^\w{6,20}@[0-9a-zA-Z]{1,10}(\.[a-z]{2,3}){1,2}$/
    if (!regEmail.test(email)) {
        alert('邮箱格式错误！')
        return
    }
    // 发送验证码请求
    axios.post(baseUrl + '/user/sendmail',{email})
    .then((res)=>{
        let {data} = res
        alert(data.msg)
    })
})

// 点击注册
$('.btn-primary').click(function (){
    // 获取用户输入的内容
    let user = $('#user').val()
    let pass = $('#pass').val()
    let nick = $('#nick').val()
    let email = $('#email').val()
    let sex = $('#sex').val()
    let age = $('#age').val()
    let code = $('#code').val()
    // 校验输入内容的格式
    let regUser = /^\w{6,18}$/
    if (!regUser.test(user)) {
        alert('账号格式错误！')
        return
    }
    let regPass = /^\w{6,12}$/
    if (!regPass.test(pass)) {
        alert('密码格式错误！')
        return
    }
    let regNick = /^.{2,16}$/
    if (!regNick.test(nick)) {
        alert('昵称格式错误！')
        return
    }
    let regEmail = /^\w{6,20}@[0-9a-zA-Z]{1,10}(\.[a-z]{2,3}){1,2}$/
    if (!regEmail.test(email)) {
        alert('邮箱格式错误！')
        return
    }
    let regCode = /^\d{2,7}$/
    if (!regCode.test(code)) {
        alert('验证码格式错误！')
        return
    }
    if (!sex) {
        alert('请选择性别！')
        return
    }
    let regAge = /^\d{1,3}$/
    if (!regAge.test(age)) {
        alert('年龄格式错误！')
        return
    }
    // 发送注册请求
    axios.post(baseUrl + '/user/register',{
        username: user,
        password: pass,
        nickname: nick,
        email,
        sex,
        age,
        code
    })
    .then((res)=>{
        let {data} = res
        alert(data.msg)
        if (data.code === 1) {
            // 注册成功，跳转到登录页面
            location.href = './login.html'
        }
    })
})
